import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/entities/payslips_entity.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'dart:math' as math;

class NormalLineChart extends StatelessWidget {
  final PayslipsEntity data;

  NormalLineChart({Key key, this.data}) : super(key: key);
  final List<Color> gradientColors = [
    Color(0xfF288E51).withOpacity(0.28),
    Color(0xfF288E51).withOpacity(0.05),
  ];

  @override
  Widget build(BuildContext context) {
    double _max = 0;
    for (final item in data.chartData) {
      _max = math.max(_max, item.value.toDouble());
    }
    return AspectRatio(
      aspectRatio: 1.2,
      child: Container(
        child: Padding(
          padding: EdgeInsets.only(
            left: getDp(20),
            right: getDp(20),
            top: getDp(10),
            bottom: getDp(10),
          ),
          child: LineChart(
            mainData(_max),
          ),
        ),
      ),
    );
  }

  LineChartData mainData(double max) {
    return LineChartData(
      maxY: max == 0 ? 12000 : null,
      // maxX: (data.chartData.length * 2).toDouble(),
      gridData: FlGridData(
        show: true,
        drawVerticalLine: false,
        getDrawingHorizontalLine: (value) {
          return FlLine(
            color: aceWhiteGreyColor,
            strokeWidth: 1,
          );
        },
      ),
      titlesData: FlTitlesData(
        show: true,
        bottomTitles: SideTitles(
          showTitles: true,
          reservedSize: 22,
          getTextStyles: (_, value) => TextStyle(
              color: aceBlackColor,
              fontWeight: FontWeight.w400,
              fontSize: getSp(12),
              height: 14 / 12),
          getTitles: (value) {
            if (value.toInt() % 2 == 0) {
              int index = value.toInt() ~/ 2;
              if (data.chartData.length > index) {
                return data.chartData[index].name;
              }
            }
            return null;
          },
          margin: 12,
        ),
        leftTitles: SideTitles(
          showTitles: true,
          getTextStyles: (_, value) => TextStyle(
            color: aceBlackColor,
            fontWeight: FontWeight.w400,
            fontSize: getSp(12),
            height: 14 / 12,
          ),
          getTitles: (double value) {
            if (value == 0) {
              return '0';
            } else if (value.abs() < 1000) {
              return '${(value).toStringAsFixed(2)}';
            } else if (1000 <= value.abs() && value.abs() < 1000000) {
              return '${(value / 1000).toStringAsFixed(2)}k';
            } else {
              return '${(value / 1000000).toStringAsFixed(2)}m';
            }
          },
          interval: max == 0 ? 3000 : data?.interval?.toDouble(),
          reservedSize: 40,
          margin: 12,
        ),
        rightTitles: SideTitles(showTitles: false),
        topTitles: SideTitles(showTitles: false),
      ),
      borderData: FlBorderData(
          show: false,
          border: Border.all(color: const Color(0xff37434d), width: 1)),
      lineBarsData: [
        LineChartBarData(
          spots: data?.chartData == null
              ? []
              : List.generate(
                      data.chartData.length,
                      (index) => _buildItem(
                          index * 2, data.chartData[index].value.toDouble()))
                  .toList(),
          isCurved: true,
          preventCurveOverShooting: true,
          colors: [gradientColors[0]],
          barWidth: 1,
          isStrokeCapRound: true,
          dotData: FlDotData(
            show: true,
            getDotPainter: _defaultGetDotPainter,
          ),
          belowBarData: BarAreaData(
            show: true,
            colors: gradientColors,
            gradientFrom: Offset(0.5, 0.0),
            gradientTo: Offset(0.5, 2.0),
          ),
        ),
      ],
    );
  }

  FlDotPainter _defaultGetDotPainter(
      FlSpot spot, double xPercentage, LineChartBarData bar, int index,
      {double size}) {
    return FlDotCirclePainter(
      radius: size,
      color: Colors.white,
      strokeColor: Color(0xFF288E51),
      strokeWidth: getDp(2),
    );
  }

  FlSpot _buildItem(int i, double value) {
    return FlSpot(i.toDouble(), value);
  }
}
